{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 导入numpy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【例】创建一个2行3列的二维数组a，创建一个包含3个数据的一维数组b，通过运算a+b, a>2, 1/b演示广播的功能\n",
    "\n",
    "【分析】用array函数创建二维数组a和的一维数组b，接着直接演示广播功能。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 5],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,3,5],[2,4,6]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1,2,3])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# a + b\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a+b演示的是二维数组和一维数组在符合规则的情况的广播，因为二维数组a的列数3和一维数组b的包含的数据个数3相等。\n",
    "$$\n",
    "\\left[ \\begin{matrix}\n",
    "\t1&\t\t3&\t\t5\\\\\n",
    "\t2&\t\t4&\t\t6\\\\\n",
    "\\end{matrix} \\right] +\\left[ 1\\ \\ 2\\ \\ 3 \\right] \\ \\rightarrow \\left[ \\begin{matrix}\n",
    "\t1&\t\t3&\t\t5\\\\\n",
    "\t2&\t\t4&\t\t6\\\\\n",
    "\\end{matrix} \\right] +\\left[ \\begin{matrix}\n",
    "\t1&\t\t2&\t\t3\\\\\n",
    "\t1&\t\t2&\t\t3\\\\\n",
    "\\end{matrix} \\right] \n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# a > 2 \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "a>2演示的是二维数组和整数的广播，这没有规则限制，实现的是二维数组的每个元素都和2比较大小。\n",
    "$$\n",
    "\\left[ \\begin{matrix}\n",
    "\t1&\t\t3&\t\t5\\\\\n",
    "\t2&\t\t4&\t\t6\\\\\n",
    "\\end{matrix} \\right] > 2 \\ \\rightarrow \\left[ \\begin{matrix}\n",
    "\t1&\t\t3&\t\t5\\\\\n",
    "\t2&\t\t4&\t\t6\\\\\n",
    "\\end{matrix} \\right] >\\left[ \\begin{matrix}\n",
    "\t2&\t\t2&\t\t2\\\\\n",
    "\t2&\t\t2&\t\t2\\\\\n",
    "\\end{matrix} \\right] \n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 1 / a \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "1/b演示的是整数和一维数组的广播，也没有规则限制，实现的是1除以一维数组的每个元素的功能。\n",
    "$$\n",
    "1/\\left[ 1\\ \\ 2\\ \\ 3 \\right] \\ \\rightarrow \\ \\left[ 1\\ \\ 1\\ \\ 1 \\right] /\\left[ 1\\ \\ 2\\ \\ 3 \\right] \n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本例选用了不同的算术运算和关系运算来观察运行的效果，你也可以自由的修改其中的运算符，观察广播功能下的运算结果。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
